package com.xoa.service.performance.leaderAssessment;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.performance.ScoreFlowMapper;
import com.xoa.dao.performance.ScoreItemMapper;
import com.xoa.dao.performance.leaderAssessment.ScoreFlowLeaderMapper;
import com.xoa.dao.performance.leaderAssessment.ScoreGroupLeaderMapper;
import com.xoa.dao.performance.leaderAssessment.ScoreItemLeaderMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.performance.*;
import com.xoa.model.performance.leaderAssessment.*;
import com.xoa.model.users.Users;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ScoreGroupLeaderService {
    @Autowired
    private ScoreGroupLeaderMapper scoreGroupLeaderMapper;

    @Autowired
    private SysCodeMapper sysCodeMapper;

    @Autowired
    private ScoreFlowLeaderMapper scoreFlowMapper;

    @Autowired
    private ScoreItemLeaderMapper scoreItemMapper;

    public ToJson addScoreGroup(ScoreGroupLeaderWithBLOBs scoreGroup, HttpServletRequest request) {
        ToJson toJson=new ToJson();
        Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
        scoreGroup.setCreateUserId(users.getUserId());
        String typeId = scoreGroup.getTypeId();
        if(typeId==null||"".equals(typeId)){
            scoreGroup.setTypeId("0");
        }
        int count=scoreGroupLeaderMapper.insertSelective(scoreGroup);
        if (count>0){
            toJson.setMsg("ok");
            toJson.setFlag(0);
        }else{
            toJson.setFlag(1);
            toJson.setMsg("err");
        }
        return toJson;
    }

    public ToJson delScoreGroupById(Integer groupId) {
        ToJson toJson=new ToJson();

        // 删除指标集
        int count=scoreGroupLeaderMapper.deleteByPrimaryKey(groupId);

        // 删除项目类型
        ScoreGroupLeaderExample scoreGroupExample =  new ScoreGroupLeaderExample();
        ScoreGroupLeaderExample.Criteria criteria = scoreGroupExample.createCriteria();
        criteria.andTypeIdEqualTo(String.valueOf(groupId));
        scoreGroupLeaderMapper.deleteByExample(scoreGroupExample);

        // 删除项目明细
        ScoreItemLeaderExample scoreItemExample = new ScoreItemLeaderExample();
        ScoreItemLeaderExample.Criteria criteria1 = scoreItemExample.createCriteria();
        criteria1.andGroupIdEqualTo(groupId);
        scoreItemMapper.deleteByExample(scoreItemExample);

        if (count>0){
            toJson.setMsg("ok");
            toJson.setFlag(0);
        }else{
            toJson.setFlag(1);
            toJson.setMsg("err");
        }
        return toJson;
    }

    public ToJson modifyScoreGroup(ScoreGroupLeaderWithBLOBs scoreGroup) {
        ToJson toJson=new ToJson();

        int count=scoreGroupLeaderMapper.updateByPrimaryKeySelective(scoreGroup);
        if (count>0){
            toJson.setMsg("ok");
            toJson.setFlag(0);
        }else{
            toJson.setFlag(1);
            toJson.setMsg("err");
        }
        return toJson;
    }

    public ToJson selScoreGroup(ScoreGroupWithBLOBs scoreGroup,Integer page,Integer pageSize,Boolean useFlag) {
        ToJson<ScoreGroupLeaderWithBLOBs> toJson=new ToJson<ScoreGroupLeaderWithBLOBs>();
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", pageParams);
        map.put("scoreGroup", scoreGroup);
        List<ScoreGroupLeaderWithBLOBs> scoreGroupList=scoreGroupLeaderMapper.selectAllBy(map);
        for (ScoreGroupLeaderWithBLOBs scoreGroupWithBLOBs:scoreGroupList){
            ScoreFlowLeaderWithBLOBs scoreFlowWithBLOBs = scoreFlowMapper.selOneByGroupId(scoreGroupWithBLOBs.getGroupId());
            if(scoreFlowWithBLOBs!=null){
                scoreGroupWithBLOBs.setEditFlag(false);
                scoreGroupWithBLOBs.setFlowName(scoreFlowWithBLOBs.getFlowTitle());
            }
        }
        if (scoreGroupList!=null){
            toJson.setObj(scoreGroupList);
            toJson.setTotleNum(pageParams.getTotal());
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }else{
            toJson.setFlag(1);
            toJson.setMsg("err");
        }
        return toJson;
    }


    public ToJson getChildCode(String parentNo) {
        ToJson<SysCode> toJson=new ToJson<>();
        List<SysCode> sysCodeList=sysCodeMapper.getChildCode(parentNo);
        if (sysCodeList!=null){
            toJson.setObj(sysCodeList);
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }else{
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }
        return toJson;
    }


    public ToJson cloneScoreGroup(Integer groupId, HttpServletRequest request) {
        ToJson toJson=new ToJson();
        Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);

        ScoreGroupLeaderWithBLOBs scoreGroup = scoreGroupLeaderMapper.selectByPrimaryKey(groupId);
        scoreGroup.setCreateUserId(users.getUserId());
        String typeId = scoreGroup.getTypeId();
        if(typeId==null||"".equals(typeId)){
            scoreGroup.setTypeId("0");
        }
        scoreGroup.setGroupId(null);
        int i =scoreGroupLeaderMapper.insertSelective(scoreGroup);

        List<ScoreGroupLeaderWithBLOBs> scoreGroupWithBLOBs = scoreGroupLeaderMapper.selectByTypeId(String.valueOf(groupId));
        for (ScoreGroupLeaderWithBLOBs scoreGroupWithBLOBs1:scoreGroupWithBLOBs) {
            List<ScoreItemLeader> scoreItems = scoreItemMapper.selectByTypeId(scoreGroupWithBLOBs1.getGroupId());
            scoreGroupWithBLOBs1.setGroupId(null);
            scoreGroupWithBLOBs1.setTypeId(String.valueOf(scoreGroup.getGroupId()));
            int i2 = scoreGroupLeaderMapper.insertSelective(scoreGroupWithBLOBs1);
            if(i2>0){
                for (ScoreItemLeader scoreItem:scoreItems) {
                    scoreItem.setItemId(null);
                    scoreItem.setGroupId(scoreGroup.getGroupId());
                    scoreItem.setType(String.valueOf(scoreGroupWithBLOBs1.getGroupId()));
                    scoreItemMapper.insertSelective(scoreItem);
                }
            }

        }

        if (i>0){
            toJson.setMsg("ok");
            toJson.setFlag(0);
        }else{
            toJson.setFlag(1);
            toJson.setMsg("err");
        }
        return toJson;
    }
}
